//
//  ARTUserEditController.m
//  ChartsDemo
//
//  Created by yunda on 2022/5/17.
//

#import "ARTUserEditController.h"

@interface ARTUserEditController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *sexLabel;
@property (weak, nonatomic) IBOutlet UILabel *heightLabel;
@property (weak, nonatomic) IBOutlet UILabel *weightLabel;
@property (weak, nonatomic) IBOutlet UILabel *ageLabel;

@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *sexTextField;
@property (weak, nonatomic) IBOutlet UITextField *heightTextField;
@property (weak, nonatomic) IBOutlet UITextField *weightTextField;
@property (weak, nonatomic) IBOutlet UITextField *ageTextField;

@end

@implementation ARTUserEditController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refresh) name:ART_Notify_LanguageChanged object:nil];
    [self refresh];
    _nameTextField.delegate = self;
    _sexTextField.delegate = self;
    _heightTextField.delegate = self;
    _weightTextField.delegate = self;
    _ageTextField.delegate = self;
}

- (void)refresh{
    self.title = FYLocalizedString(@"个人资料", @"");
    _nameLabel.text = FYLocalizedString(@"姓名：", @"");
    _sexLabel.text = FYLocalizedString(@"性别：", @"");
    _heightLabel.text = FYLocalizedString(@"身高：", @"");
    _weightLabel.text = FYLocalizedString(@"体重：", @"");
    _ageLabel.text = FYLocalizedString(@"年龄：", @"");
    _nameTextField.placeholder = FYLocalizedString(@"请输入姓名", @"");
    _sexTextField.placeholder = FYLocalizedString(@"请选择性别", @"");
    _heightTextField.placeholder = FYLocalizedString(@"请输入身高", @"");
    _weightTextField.placeholder = FYLocalizedString(@"请输入体重", @"");
    _ageTextField.placeholder = FYLocalizedString(@"请输入年龄", @"");
    
    ARTUserModel *model = [ARTUserModel getUser];
    _nameTextField.text = SAFE_STRING(model.name);
    _sexTextField.text = SAFE_STRING(model.sex).length == 0 ? FYLocalizedString(@"sleep_watch_woman", @"") : SAFE_STRING(model.sex);
    _heightTextField.text = SAFE_STRING(model.height);
    _weightTextField.text = SAFE_STRING(model.weight);
    _ageTextField.text = SAFE_STRING(model.age);
}

- (IBAction)okAction:(id)sender {
    if (_nameTextField.text.length == 0) {
        [QMUITips showWithText:_nameTextField.placeholder];
        return;
    }
    if (_sexTextField.text.length == 0) {
        [QMUITips showWithText:_sexTextField.placeholder];
        return;
    }
    if (_heightTextField.text.length == 0) {
        [QMUITips showWithText:_heightTextField.placeholder];
        return;
    }
    if (_weightTextField.text.length == 0) {
        [QMUITips showWithText:_weightTextField.placeholder];
        return;
    }
    if (_ageTextField.text.length == 0) {
        [QMUITips showWithText:_ageTextField.placeholder];
        return;
    }
    ARTUserModel *model = [[ARTUserModel alloc]init];
    model.name = _nameTextField.text;
    model.sex = _sexTextField.text;
    model.height = _heightTextField.text;
    model.weight = _weightTextField.text;
    model.age = _ageTextField.text;
    [model storeUser];
    [QMUITips showLoadingInView:self.view hideAfterDelay:0.5];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.navigationController popViewControllerAnimated:YES];
    });
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    if ([textField isEqual:_sexTextField]) {
        [self.view endEditing:YES];
        NSArray *titles = @[FYLocalizedString(@"sleep_watch_man", @""),FYLocalizedString(@"sleep_watch_woman", @"")];
        [self lq_showAlertSheetControllerWithTitles:titles handles:^{
            textField.text = titles[0];
        }, ^{
            textField.text = titles[1];
        }, nil];
        return NO;
    }
    return YES;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesBegan:touches withEvent:event];
    [self.view endEditing:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
